var weekTemplateListUrl = basePath + '/sys/dict/weekTemplate/list';
var weekTemplateByIdUrl = basePath + "/sys/dict/weekTemplate/id";
var addUpdateWeekTemplateUrl = basePath + "/sys/dict/weekTemplate/addUpdate";
var termListUrl = basePath + "/sys/dict/termList";

var confirmDialog;
var dataGrid;
$(function() {
	initTermList();
});
function initTermList(){
    $.ajax({
        url: termListUrl,
        type: 'POST',
        dataType: 'json',
        error : function(data) {
            $.messager.progress('close');
            $.messager.alert('系统提示', data.msg, 'error');
        },
        success: function (data) {
        	var html = "";
        	for(var i in data) {
        		if(i == 0) {
        			loadData(data[i].id);
        		}
        		html += "<option value='"+data[i].id+"'>"+data[i].termName+"</option>";
        	}
            $("#termId").html(html);
        }
    });

}
function loadData(termId) {
	dataGrid = $('#teacherManageDg').datagrid({
		url : weekTemplateListUrl,
		toolbar: '#tb',
		fit : true,
		fitColumns : true,
		border : true,
		pagination : false,
		idField : 'id',
		pageSize : config.pageSize,
		autoRowHeight : 140,
		pageList : config.pageList,
		nowrap : true,
		striped : true,
		rownumbers : true,
		queryParams : {
            "termId": termId
        },
		columns : [ [  {
			field : 'id',
			width : 20,
			align : "center",
			checkbox : true
		}, {
			field : 'weekNo',
			title : '<b>周期</b>',
			resizable : true,
			align : "center",
			width : 20,
			editor:{
				type:'numberbox',
				options:{
					required:true
				}
			}
		}, {
			field : 'isValid',
			align : 'center',
			title : '<b>状态</b>',
			width : 20,
        	formatter:function(value,row,index){
                if(value == 1){
                    return "启用";
                }else{
                    return "禁用";
                }
            },
            editor:{
				type:'combobox',
				options: {  
                    data: [{"value": 1, "text": "启用"},{"value": 0, "text": "禁用"}],  
                    valueField: "value",  
                    textField: "text",  
                    editable: false,  
                    panelHeight:70,  
                    required: true  
                }  
			}
        }, {
			field : 'startDate',
			title : '<b>开始时间</b>',
			width : 30,
			align : "center",
			editor:{
				type:'datebox',
				options:{
					required:true
				}
			}
		}, {
			field : 'endDate',
			title : '<b>结束时间</b>',
			resizable : true,
			align : "center",
			width : 30,
			editor:{
				type:'datebox',
				options:{
					required:true
				}
			}
		}, {
			field : 'createTime',
			title : '<b>创建时间</b>',
			resizable : true,
			align : "center",
			width : 30,
			formatter : function(value,row,index){
				return getTime(value, "yyyy-MM-dd hh:mm");
			}
		}, {
			field : 'updateTime',
			title : '<b>修改时间</b>',
			resizable : true,
			align : "center",
			width : 30,
			formatter : function(value,row,index){
				return getTime(value, "yyyy-MM-dd hh:mm");
			}
		}] ],
		onAfterEdit : function(rowIndex, rowData, changes){
			if(rowData.id){
				changes.id = rowData.id;
				addmodify(changes)
			}else {
				addmodify(rowData);
			}
		}
	});
}

function reload() {
	dataGrid.datagrid("clearSelections");
	dataGrid.datagrid('load');
}

function searchForm(formId) {
	dataGrid.datagrid('load', $.serializeObject($('#' + formId)));
}
function resetForm(formId) {
	$("#" + formId)[0].reset();
}

function save(id) {
	if(dgEditIndex[id] != null){
		$('#' + id).datagrid('endEdit', dgEditIndex[id]);
		dgEdit[id] = null;
	}
}

function addmodify(data) {
	$.ajax({
	   type: "POST",
	   url: addUpdateWeekTemplateUrl,
	   data: data,
	   success: function(res){
		   if (res.code == '000'){
			   reload();
			   $.messager.alert('提示', '提交成功', 'info');
		   }else{
			   $.messager.alert('提示', '提交失败', 'error');
			   errorEdit("teacherManageDg");
		   }
		},
		error: function() {
			alert("提交异常");
			errorEdit("teacherManageDg");
		}
	});
}

function upload() {
	$('#uploadBox').dialog({
		title : '导入模板',
		modal : true,
		width : 400,
		top : 100,
		draggable : true,
		resizable : true,
		buttons : '#btns',
		onClose : function() {
			cleanFormPanel("uploadForm");
		}
	}).show();
}

var cleanFormPanel=function(formId){
	$('#' + formId)[0].reset();
	$('#' + formId + " #imgUrl").val(null);
	$('#' + formId + " #id").val(null);
}

function uploadCommit(){
	var fileObj = document.getElementById('filebox_file_id_1').files[0];
	var xhr = new XMLHttpRequest();
	xhr.open('POST', basePath + "/sys/file/weekTemplate/import");
	xhr.onload = function () {
       if (xhr.status === 200) {
	       	var resp = JSON.parse(xhr.response);
	       	$("#uploadTheFile").val(null);
	       	if(resp.code == '000') {
	       		window.location.reload();
	       	}else {
	       		alert(resp.msg);
	       	}
       }
   };
   xhr.onerror = function () {
   		alert("上传异常");
   };
   var fd = new FormData();
   fd.append('files', fileObj);
   fd.append("termId", $("#termId").val());
   // 触发上传
   xhr.send(fd);
}

